﻿using System;
using System.Linq;
using xycn.Base;
using xycn.IAuthorize;
using xycn.pluginsupport.BaseHelperPlugins;

namespace xycn.pluginsupport.ControllerPlugins
{
    /// <summary>
    /// controller基类
    /// </summary>
    [ApiController]
    public class BaseController
    {

        //错误代码
        protected int _execCode = 0;
        public int execCode { get { return _execCode; } }

        /// <summary>
        /// 错误信息
        /// </summary>
        protected string _errorInfor = "";
        public string errInfo { get { return _errorInfor; } }

        /// <summary>
        /// 用于保存HttpContext相关信息
        /// </summary>
        public ControllerContext controllerContext = new ControllerContext();

        /// <summary>
        /// 返回基类封装的对象数据实体
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="retcnt"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ReponseContext _success(object data, int code = 0, string msg = "操作成功", int retcnt = 0)
        {
            return new ReponseContext() { Data = data, Message = msg, RowCnt = retcnt, ErrorCode = code };
        }

        /// <summary>
        /// 返回基类封装的对象数据实体
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="retcnt"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ReponseContext _success(Result<object> data, int code = 0, string msg = "操作成功", int retcnt = 0)
        {
            return new ReponseContext() { Data = data, Message = msg, RowCnt = retcnt, ErrorCode = code };
        }

        /// <summary>
        /// 操作失败 统一返回
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="errormsg"></param>
        /// <param name="errorcode"></param>
        /// <returns></returns>
        public ReponseContext _error(Exception ex, string errormsg, int errorcode = 500)
        {
            //统一写日志
            //Log.Write()
            Console.WriteLine(ex.Message);
            return new ReponseContext() { Message = ex.Message, ErrorCode = errorcode };
        }

        /// <summary>
        /// 操作失败 统一返回
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="errormsg"></param>
        /// <param name="errorcode"></param>
        /// <returns></returns>
        public ReponseContext _fail(string errormsg, int errorcode = 500)
        {
            //统一写日志
            //Log.Write()
            Console.WriteLine(errormsg);
            return new ReponseContext() { Message = errormsg, ErrorCode = errorcode };
        }

        //public SignInfo GetAuthInfo(string token)
        //{
        //    return AuthHelper.GetSignInfo(token);
        //}

        public SignInfo GetAuthInfo()
        {
            var _token = controllerContext.requestInfo.headersList.FirstOrDefault(p => p.Key == "Authorization").Value;

            if (!string.IsNullOrWhiteSpace(_token))
            {
                return AuthHelper.GetSignInfo(_token);
            }
            return null;
        }

    }

}
